<?php
namespace app\api\validate;
use think\Db;
use think\Validate;

/**
 *
 * @group_name
 *
 * @author Master
 *
 * @api ${API}
 *
 * @time 2020/6/3 - 16:34
 *
 */
class UserRegister extends Validate{

    protected $rule = [
        'username' => 'require|register_check:username',
        'password' => 'require|min:6|max:16',
        'repassword' => 'require|min:6|max:16|register_check:repassword'
    ];

    protected $message = [
        'username.require' => '用户名不能为空',
        'password.require' => '密码不能为空',
        'password.min' => '密码长度最短为六位',
        'password.max' => '密码最长为十六位',
        'repassword.require' =>'重复密码不能为空',
        'repassword.min' => '重复密码长度最短为六位',
        'repassword.max' => '重复密码最长为十六位',
    ];

    //判断当前登录的用户是否存在，并通过md5解密
    public function register_check($value,$rule)
    {
        $username = input('username');
        $password = input('password');
        switch ($rule) {
            case 'username':
                $result = Db::name("account")->where(["username" => $value, "data_state" => 1])->find();
                if ($result) {
                    return "用户已存在";

                } else {
                    return true;
                }
                break;
            case "repassword":
                if ($value != $password){
                    return "两次密码不相同，请重新输入密码";
                }else{
                    $map = [
                        "username" => $username,
                        "password" => password_md5($value),
                        "data_state" => 1,
                        "create_time" =>date("Y-m-d H:i:s", time()),
                        "update_time" => date("Y-m-d H:i:s", time())
                    ];
                    $insetUserinfo = Db::name("account")->insert($map);
                    if ($insetUserinfo) {
                        return true;
                    } else {
                        return '注册失败';
                    }
                }
                break;
            default:
                return "系统错误";
                break;
        }
    }


}